/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.PC;

import csse3004.GD.DA.PollCreatorDA;
import csse3004.GD.MCAnswer;
import csse3004.GD.Question;
import csse3004.GD.SRAnswer;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Lewis Kelly 42007689
 */
@ Controller
public class viewAnswersController {

    protected final Log logger = LogFactory.getLog(getClass());
    private PollCreatorService pollCreatorService = new PollCreatorDA();
    String fail = "No Answers";
    ModelAndView thing = new ModelAndView("viewAnswers", "fail", fail);

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(HttpSession session, HttpServletRequest hsr) throws Exception {

        if (hsr.getParameter("qid") != null && hsr.getParameter("type") != null) {
            int qid = Integer.parseInt(hsr.getParameter("qid"));
            String type = hsr.getParameter("type");
            ArrayList<MCAnswer> MCqList = new ArrayList<MCAnswer>();
            ArrayList<SRAnswer> SRqList = new ArrayList<SRAnswer>();
            int type2 = 0;
            //Question ques = pollCreatorService.getQuestion(qid);
            
            Question text = pollCreatorService.getQuestion(qid);
            String stuff = text.getText();
            logger.info(stuff);
            if (type.equals("MC")) {
                MCqList = pollCreatorService.getMCAnswers(qid);
                ModelAndView mnv = new ModelAndView("viewAnswers", "answers", MCqList);
                mnv.addObject("question", stuff);
                mnv.addObject("type", type2);
                mnv.addObject("qids", qid);
                return mnv;

            } else {
                type2 = 1;
                SRqList = pollCreatorService.getSRKeywords(qid);
                logger.info(SRqList.isEmpty());
                ModelAndView mnv = new ModelAndView("viewAnswers", "answers", SRqList);
                mnv.addObject("question", stuff);
                mnv.addObject("type", type2);
                mnv.addObject("qids", qid);
                return mnv;
            }
        } else {
            return thing;
        }

    }
}
